﻿using WebAssistant.WinUI.Properties;
using Sunny.UI;
using Sunny.UI.Properties;
using System;
using System.ComponentModel;
using System.Reflection;
using static Sunny.UI.UILoginForm;

namespace WebAssistant.WinUI
{
    [DefaultProperty("Text")]
    [DefaultEvent("OnUnlock")]
    public partial class UILockScreen : UIForm
    {
        public UILockScreen()
        {
            InitializeComponent();
        }

        public string Title
        {
            get => lblTitle.Text;
            set => lblTitle.Text = value;
        }

        public string SubText
        {
            get => lblSubText.Text;
            set => lblSubText.Text = value;
        }

        private UILoginImage loginImage;

        [DefaultValue(UILoginImage.Login1)]
        public UILoginImage LoginImage
        {
            get => loginImage;
            set
            {
                if (loginImage != value)
                {
                    loginImage = value;

                    if (loginImage == UILoginImage.Login1) BackgroundImage = Resources.Login1;
                    if (loginImage == UILoginImage.Login2) BackgroundImage = Resources.Login2;
                    if (loginImage == UILoginImage.Login3) BackgroundImage = Resources.Login3;
                    if (loginImage == UILoginImage.Login4) BackgroundImage = Resources.Login4;
                    if (loginImage == UILoginImage.Login5) BackgroundImage = Resources.Login5;
                    if (loginImage == UILoginImage.Login6) BackgroundImage = Resources.Login6;
                }
            }
        }

        private void btnUnlock_Click(object sender, EventArgs e)
        {
            if (ButtonUnlockClick != null)
            {
                ButtonUnlockClick?.Invoke(sender, e);
            }
            else
            {
                IsUnlock = OnUnlock != null && OnUnlock(edtPassword.Text);
                if (IsUnlock) Close();
            }
        }

        public event EventHandler ButtonUnlockClick;

        [DefaultValue(false), Browsable(false)]
        public bool IsUnlock { get; protected set; }

        public delegate bool OnUnlockHandle(string password);

        public event OnUnlockHandle OnUnlock;

        private void edtPassword_DoEnter(object sender, EventArgs e)
        {
            btnUnlock.PerformClick();
        }

        public string Password
        {
            get => edtPassword.Text;
            set => edtPassword.Text = value;
        }
    }
}
